Skip to content

fix(cli): surface actionable re-auth message on revoked cloud API key#11

Open
andrei-hasna wants to merge 1 commit into
mainfrom
fix/cloud-revoked-key-reauth
Open

fix(cli): surface actionable re-auth message on revoked cloud API key#11
andrei-hasna wants to merge 1 commit into
mainfrom
fix/cloud-revoked-key-reauth

Conversation

@andrei-hasna

Copy link
Copy Markdown
Contributor

What

Cloud list / add (config create) — and every async CLI action — no longer crash with a raw CloudHttpError stack trace when the cloud API key is revoked or invalid. Auth failures (HTTP 401/403) now print a clear, actionable re-auth message and exit 1.

Why

On the Infinity global-rule update, @hasna/instructions in cloud mode (HASNA_INSTRUCTIONS_API_URL + HASNA_INSTRUCTIONS_API_KEY set) failed the list/add/create paths with CloudHttpError 401 API key has been revoked, blocking the managed-rule update (which had to fall back to @hasna/configs).

Two root causes:

  1. The CLI used the synchronous program.parse, which does not await or catch async action rejections — so a rejected action surfaced as an unhandled-promise raw stack trace.
  2. list and add (and several other actions) had no try/catch at all.

Fix

  • Switch to program.parseAsync(...) with a single top-level handler that routes every uncaught action error through formatCliError.
  • Add isCloudAuthError (401/403 CloudHttpError) and formatCliError in src/data/config-store.ts. Auth failures now render:
    • what happened (auth failed for the configured API URL),
    • and two repair paths: rotate HASNA_INSTRUCTIONS_API_KEY, or unset both env vars to use the local store.
  • Route existing per-command catch blocks through formatCliError too (non-auth errors unchanged).

Tests

  • New describe("revoked / invalid API key handling") in src/data/config-store.test.ts: isCloudAuthError matching, formatCliError output, and revoked-key coverage for both the cloud list and create paths.
  • bun test: 287 pass / 0 fail (clean env). tsc --noEmit: clean.
  • End-to-end smoke against the live API with an invalid key: list and add now print the re-auth message and exit 1; valid-key and local-mode paths unaffected.

Refs: todos OPE80-00013

Cloud create/list (and every async CLI action) previously crashed with a
raw CloudHttpError stack trace when HASNA_INSTRUCTIONS_API_KEY was revoked
or invalid: program.parse does not await/catch async action rejections and
list/add had no try/catch, so the managed-rule update path was blocked.

Switch the CLI to program.parseAsync with a single top-level handler and add
formatCliError + isCloudAuthError helpers. 401/403 auth failures now print a
clear re-auth path (rotate HASNA_INSTRUCTIONS_API_KEY, or unset both env vars
to fall back to the local store) instead of a raw stack trace. Non-auth errors
are unchanged. Adds unit + create/list revoked-key coverage.

Refs: todos OPE80-00013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant